package com.example.demo_news.until;

import com.example.demo_news.Repository.UserPepository;
import com.example.demo_news.entity.MyUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

//获取当前登录的用户
@Component
public class UserUntil {
    @Autowired 
    private UserPepository userPepository;
    public MyUser getUser(){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userPepository.findUserByusername(userDetails.getUsername());
    }
}
